package com.zbkj.crmeb.store.service.impl;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.constants.Constants;
import com.crmeb.core.utils.RedisUtil;
import com.zbkj.crmeb.enums.MarkupRateTypeEnum;
import com.zbkj.crmeb.store.dao.MarkupRateDao;
import com.zbkj.crmeb.store.model.MarkupRate;
import com.zbkj.crmeb.store.request.MarkupRateRequest;
import com.zbkj.crmeb.store.service.MarkupRateService;

/**
* @author cyz
* @description MarkupRateServiceImpl 接口实现
* @date 2021-05-19
*/
@Service
public class MarkupRateServiceImpl extends ServiceImpl<MarkupRateDao, MarkupRate> implements MarkupRateService {

    @Resource
    private MarkupRateDao dao;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public BigDecimal selectMerchantRatio(MarkupRateRequest request) {
        if(request.getType() == MarkupRateTypeEnum.PLAT_平台加价率.getType()){
            if(null!=redisUtil.get(Constants.PLAT_RATIO+request.getMerId())){
                BigDecimal ratio = (BigDecimal) redisUtil.get(Constants.PLAT_RATIO+request.getMerId());
                return ratio;
            }
            LambdaQueryWrapper<MarkupRate> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(MarkupRate::getMerId, request.getMerId());
            lambdaQueryWrapper.eq(MarkupRate::getType, MarkupRateTypeEnum.PLAT_平台加价率.getType());
            lambdaQueryWrapper.last("limit 1");
            MarkupRate markupRate =  dao.selectOne(lambdaQueryWrapper);
            if(null!=markupRate){
                redisUtil.set(Constants.PLAT_RATIO + request.getMerId(),markupRate.getRatio(), 60L, TimeUnit.MINUTES);
                return markupRate.getRatio();
            }
        }else{
            if(null!=redisUtil.get(Constants.MERCHANT_RATIO+request.getMerId())){
                BigDecimal ratio = (BigDecimal) redisUtil.get(Constants.MERCHANT_RATIO+request.getMerId());
                return ratio;
            }
            LambdaQueryWrapper<MarkupRate> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(MarkupRate::getMerId, request.getMerId());
            lambdaQueryWrapper.eq(MarkupRate::getType, MarkupRateTypeEnum.MERCHANT_商户加价率.getType());
            MarkupRate markupRate = dao.selectOne(lambdaQueryWrapper);
            if(null!=markupRate){
                redisUtil.set(Constants.MERCHANT_RATIO + request.getMerId(), markupRate.getRatio(), 60L, TimeUnit.MINUTES);
                return markupRate.getRatio();
            }
        }
        return BigDecimal.ZERO;
    }

    @Override
    public MarkupRate selectMarkupRate(MarkupRateRequest request) {
        LambdaQueryWrapper<MarkupRate> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MarkupRate::getMerId, request.getMerId());
        lambdaQueryWrapper.eq(null != request.getType(), MarkupRate::getType, request.getType());
        return dao.selectOne(lambdaQueryWrapper);
    }
}

